import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:qunaimedical/utils/common_util.dart';
import 'package:qunaimedical/values/configs.dart';

typedef Callback<T> = void Function(T value);

class UserTypeBean {
  String type, name;

  UserTypeBean({
    required this.type,
    required this.name,
  });

  @override
  String toString() {
    return this.name;
  }
}

/// 用户类型选择器
class UserTypeSelector extends StatefulWidget {
  final Callback<UserTypeBean> onChanged;

  const UserTypeSelector({
    Key? key,
    required this.onChanged,
  }) : super(key: key);

  @override
  _UserTypeSelectorState createState() => _UserTypeSelectorState();
}

class _UserTypeSelectorState extends State<UserTypeSelector> {
  UserTypeBean type = userTypes.first;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        CommonUtil.hideKeyboard(context);
        _pick();
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            margin: EdgeInsets.only(top: 16),
            child: Text(
              "用户类型",
              style: const TextStyle(fontSize: 16),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16.0),
            child: Row(
              children: [
                Expanded(
                  child: Text(
                    type.name,
                    style: const TextStyle(fontSize: 16),
                  ),
                ),
                Container(
                  child: Icon(
                    Icons.keyboard_arrow_down,
                    color: Colors.grey,
                  ),
                  margin: EdgeInsets.only(right: 8),
                ),
              ],
            ),
          ),
          Container(
            color: Colors.grey[300],
            height: 1,
          ),
        ],
      ),
    );
  }

  void _pick() {
    CommonUtil.showPicker(
      context,
      pickerData: userTypes,
      onConfirm: (Picker picker, List<int> selected) {
        int index = selected[0];
        widget.onChanged(userTypes[index]);
        setState(() {
          type = userTypes[index];
        });
      },
      selectedIndex: [userTypes.indexOf(type)],
    );
  }
}
